import QQMapWX from '@/common/qqmap-wx-jssdk.js';
import {
	mapState,
	mapMutations
} from 'vuex';

class Location{
	constructor(arg) {
		
	}
	
	// ...mapMutations(['SET_LOCATION']),
	getLocation(e) {
		   return new Promise((resolve,reject)=>{
			   uni.authorize({
			   		scope: "scope.userLocation",
			   		success: (res) => {
			   			let qqmapsdk = new QQMapWX({
			   				// 填写自己的Key值，这个值是与AppID绑定的
			   				key: 'Y22BZ-HHIC2-MVOU4-CH2HX-YVBG7-ZEFJB'
			   			});
			   			uni.getLocation({
			   				type: 'gcj02',
			   				success: (res) => {
			   					qqmapsdk.reverseGeocoder({
			   						location: {
			   							latitude: res.latitude,
			   							longitude: res.longitude
			   						},
			   						success: (res) => {							
			   							res.name = res.result.ad_info.district;
			   							res.citycode = res.result.ad_info.adcode;
			   							res.province=res.result.ad_info.province;
			   							res.city=res.result.ad_info.city;
			   							// this.myCityObj = res;
			   							// this.hasLocation = true;
			   							// this.SET_LOCATION(res)
			   						    resolve(res)
			   						},
			   						fail: (err) => {
										console.log(err)
										reject(err)
			   							uni.showToast({
			   								icon: 'none',
			   								title: '获取用户定位失败，请手动选择当前城市'
			   							})
			   						}
			   					})
			   				}
			   			})
			   		},fail:(err)=> {
			   			uni.showModal({
			   				content: '检测到您还没有打开位置授权，是否去设置打开？',
			   				confirmText: "确认",
			   				cancelText: "取消",
			   				success: function(res) {
			   					if (res.confirm) {
			   						uni.openSetting({
			   							success: (res) => {
			   			
			   							}
			   						})
			   					} else {
			   						console.log('用户点击取消')
			   					}
			   				}
			   			})
			   		}
			   })
		   })
		
			
	}
	
}

export default Location;